package com.endurance.controller;

import com.endurance.po.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Value("${server.port}")
    private int port;

    @GetMapping("/findAll")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "服务调用成功,服务提供端口为: " + port);
        map.put("status", true);
        System.out.println("商品服务当前端口号：" + port);
        return map;
    }

    @GetMapping("/get/variable")
    public Map<String, Object> method1(String name) throws InterruptedException {

//        Thread.sleep(1000); // 测试OpenFeign的超时时间
        if ("xiaobai".equals(name)) {
            throw new RuntimeException();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "服务调用成功,服务提供端口为: " + port);
        map.put("status", true);
        map.put("name", name);
        return map;
    }

    @GetMapping("/get/obj")
    public Map<String, Object> method2(Product product) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "服务调用成功,服务提供端口为: " + port);
        map.put("status", true);
        map.put("商品名称", product.getName());
        map.put("商品价格", product.getPrice());
        return map;
    }

    @PostMapping("/post/variable")
    public Map<String, Object> method3(String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "服务调用成功,服务提供端口为: " + port);
        map.put("status", true);
        map.put("name", name);
        System.out.println("商品服务当前端口号：" + port);
        return map;
    }

    @PostMapping("/post/obj")
    public Map<String, Object> method4(@RequestBody Product product) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "服务调用成功,服务提供端口为: " + port);
        map.put("status", true);
        map.put("商品名称", product.getName());
        map.put("商品价格", product.getPrice());
        return map;
    }


}
